<?php

namespace support\strategy\sms\impl;

use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
use support\Log;
use support\strategy\sms\SmsInterface;
use Hhink\WebmanSms\Sms;
use Overtrue\EasySms\EasySms;
use think\Validate;

class AliSmsImpl implements SmsInterface
{


    public function send($param)
    {

        if(!$param["accessKeyId"] ||  !$param["accessKeySecret"]  ||  !$param["signName"]){
            return dataReturn(-1, '未短信配置');
        }
        $config = config('plugin.hhink.webman-sms.app');
        $config['gateways']['aliyun']['access_key_id'] = $param["accessKeyId"];
        $config['gateways']['aliyun']['access_key_secret'] = $param["accessKeySecret"];
        $config['gateways']['aliyun']['sign_name'] = $param["signName"];
        $sms = new EasySms($config);
        try {
            $sms->send($param['phone'], [
                'content' => "您的登录验证码: {$param['code']}",
                'template' => $param['templateCode'],
                'data' => [
                    'code' => json_decode($param['code'],true)['code']
                ],
            ], ['aliyun']);
        } catch (NoGatewayAvailableException $error) {
            Log::error("发送阿里云短信失败：" . $error->getException('aliyun'));
            return dataReturn(-1, '发送失败');
        }

        return dataReturn(0, '发送成功');
    }
}
